package site.marqstree.kotlin.library.base.ui.widget.search.activity

import android.os.Bundle
import android.support.v4.media.MediaBrowserCompat
import android.view.View
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.SPStaticUtils
import kotlinx.android.synthetic.main.activity_search.*
import org.jetbrains.anko.toast
import site.marqstree.kotlin.library.base.config.constant.SearchHistorySPKey
import site.marqstree.kotlin.library.base.extent.onClick
import site.marqstree.kotlin.library.base.ui.R
import site.marqstree.kotlin.library.base.ui.activity.BaseActivity
import site.marqstree.kotlin.library.base.ui.widget.search.activity.recyclerview.SearchHistoryAdapter
import site.marqstree.kotlin.library.base.util.callback.CallbackManager
import site.marqstree.kotlin.library.base.util.callback.CallbackType
import site.marqstree.kotlin.library.base.util.callback.IGlobalCallback


/*
 * 项目名: KotlinMall
 * 包名: site.marqstree.kotlin.library.base.ui.widget.search
 * 文件名: ActivitySearch
 * 创建者: marqstree
 * 创建时间: 2020/3/9 16:07
 * 描述: 关键字搜索商品页
 */
class ActivitySearch : BaseActivity(), View.OnClickListener {
    private var mAdapter = SearchHistoryAdapter(mutableListOf())

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_search)

        initRecyclerView()

        initView()
    }

    override fun onStart() {
        super.onStart()
        loadData()
    }

    private fun initRecyclerView(){
        //设置recyclerview为垂直方向滚动布局
        val linearLayoutManager =
            LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        mSearchHistoryRv.setLayoutManager(linearLayoutManager)
    }

    /*
        初始化视图
     */
    private fun initView() {

        mLeftIv.setOnClickListener(this)
        mSearchTv.setOnClickListener(this)
        mClearBtn.setOnClickListener(this)

        mSearchHistoryRv.layoutManager = LinearLayoutManager(this)
        mSearchHistoryRv.adapter = mAdapter
    }

    /*
        从SP中加载数据
     */
    private fun loadData() {
        val set = SPStaticUtils.getStringSet(SearchHistorySPKey.KEY_SP_SEARCH_HISTORY)
        mNoDataTv.visibility = if(set.isEmpty()) View.VISIBLE else View.GONE

        mDataView.visibility = if(set.isNotEmpty()) View.VISIBLE else View.GONE
        if (set.isNotEmpty()) {
            val list = mutableListOf<String>()
            list.addAll(set)
            mAdapter.setNewData(list)
        }
    }

    override fun onClick(v: View) {
        when (v.id) {
            //返回按钮
            R.id.mLeftIv -> finish()
            //执行搜索
            R.id.mSearchTv -> doSearch()
            //清除历史记录
            R.id.mClearBtn -> {
                SPStaticUtils.remove(SearchHistorySPKey.KEY_SP_SEARCH_HISTORY)
                loadData()
            }
        }
    }

    //搜索
    private fun doSearch() {
        if (mKeywordEt.text.isNullOrEmpty()) {
            toast("请输入需要搜索的关键字")
        } else {
            val inputValue = mKeywordEt.text.toString()

            val historis: Set<String> = SPStaticUtils.getStringSet(SearchHistorySPKey.KEY_SP_SEARCH_HISTORY)
            val newHistories = historis+inputValue
            SPStaticUtils.put(SearchHistorySPKey.KEY_SP_SEARCH_HISTORY,newHistories)
            enterActivityList(inputValue)
        }
    }

    /*
        调用回调接口
        进入列表界面
     */
    private fun enterActivityList(value: String) {
        val callback:IGlobalCallback<String>? = CallbackManager.instance
                    .getCallback(CallbackType.SEARCH_HISTORY) as IGlobalCallback<String>
        callback?.executeCallback(value)
    }

}
